package com.app.demo.controller;

import com.app.core.mvc.repeat.RepeatCommitCheck;
import com.app.core.mvc.result.Result;
import com.app.core.mvc.serialization.argument.DecryptPathVariable;
import com.app.core.security.ResourceOwnerContext;
import com.app.core.security.ResourceOwnerService;
import com.app.core.signature.Signature;
import com.app.demo.entity.User;
import com.app.demo.mapper.UserMapper;
import com.app.demo.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author qiangt
 * @date 2023/9/13
 * @apiNote
 */
@Signature
@Slf4j
@RestController
@RequestMapping("/signature")
@RequiredArgsConstructor
public class SignatureController {

    private final UserService service;
    private final UserMapper mapper;

    private final ResourceOwnerService resourceOwnerService;

    private final ResourceOwnerContext resourceOwnerContext;

    @PostMapping()
    public Result<User> save(@RequestBody User user) {
        return Result.success(service.insert(user));
    }

    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        service.delete(id);
        return Result.success();
    }

    @DeleteMapping("/list/{ids}")
    public Result<Void> deleteList(@DecryptPathVariable List<Long> ids) {
        service.delete(ids);
        return Result.success();
    }

    @PutMapping()
    public Result<User> edit(@RequestBody User user) {
        return Result.success(service.edit(user));
    }

    @GetMapping("/{id}")
    public Result<User> getById(@PathVariable Long id) {
        User user = service.getById(id);
        log.info("查询用户成功：{}", user);
        return Result.success(user);
    }

    @RepeatCommitCheck
    @PostMapping("/pageList/{pageSize}/{pageNumber}")
    public Result<List<User>> pageList(@RequestBody User user, @PathVariable Long pageSize, @PathVariable Long pageNumber) {
        return Result.success(service.pageList(user, pageSize, pageNumber));
    }

    @PostMapping("/import")
    public void testImport(MultipartFile file) {
        log.info("Test 导入");
    }
}
